package com.example.mobilechannel;

import java.io.IOException;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

public class MobileChannel extends Activity implements Runnable {

	public void run() {
		// TODO Auto-generated method stub
		
		try {
			content = cp.read();
			if((null != content) && (!"".equals(content)))
			{
				cs.connect();
				cs.write(content);
				content = cs.read();
				cp.write(content);
				cp.stop();
				
				mHandler.sendMessage(mHandler.obtainMessage());   
			}
			
			new Thread(MobileChannel.this).start();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			//showDialog(e.getMessage());s
		}
	}

	private ConnectServer cs;
	private ConnectPos cp;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mobile_channel);
        
        findViews();
        restorePrefs();
        setListener();
        
        cp = new ConnectPos();
        try {
			cs = new ConnectServer("192.168.1.2", 5000);
			fieldView_hostaddr.setText(cs.getHostAddr());
			fieldView_port.setText(Integer.toString(cs.getPort()));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			showDialog(e.getMessage());
		}

		new Thread(MobileChannel.this).start();
    }
    
    @Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();

	}

    @Override
	protected void onResume() { 
		// TODO Auto-generated method stub
		super.onResume();

	}

	private EditText fieldView_hostaddr;
    private EditText fieldView_port;
    private TextView textView_receivedData;
    private Spinner spinner_sendData;
    private Button button_send;
    private String content;
    ArrayAdapter<CharSequence> adp_sendData;
    
	private void findViews() {
		fieldView_hostaddr = (EditText)findViewById(R.id.editText_hostaddr);
		fieldView_port = (EditText)findViewById(R.id.editText_port);
		textView_receivedData = (TextView)findViewById(R.id.textView_received);
		spinner_sendData = (Spinner)findViewById(R.id.spinner_senddata);
		button_send = (Button)findViewById(R.id.button_send);
		
		adp_sendData = ArrayAdapter.createFromResource(
													this, 
													R.array.mc_senddata, 
													android.R.layout.simple_spinner_item);
		adp_sendData.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner_sendData.setAdapter(adp_sendData);
		
    }
    
    private void restorePrefs() {
    	
    }
    
    private void setListener() {
    	button_send.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String inputHost, inputPort;
				
				inputHost = fieldView_hostaddr.getText().toString();
				inputPort = fieldView_port.getText().toString();
				if((!"".equals(inputHost)) && (!"".equals(inputPort)))
				{
					cs.setHost(inputHost, Integer.parseInt(inputPort));
				}
				cp.start();
			}
		});
    	
    	spinner_sendData.setOnItemSelectedListener(getSendData);
    }
    
    private OnItemSelectedListener getSendData = new OnItemSelectedListener() {

		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			// TODO Auto-generated method stub
			cp.setData(adp_sendData.getItem((int) spinner_sendData.getSelectedItemId()).toString());
			cp.start();
		}

		public void onNothingSelected(AdapterView<?> arg0) {
			// TODO Auto-generated method stub
			
		}
    	
	};

    public void showDialog(String msg) {
    	new AlertDialog.Builder(this)
    		.setTitle("Error")
    		.setMessage(msg)
    		.setPositiveButton(R.string.mc_dlg_ok, new DialogInterface.OnClickListener() {
				
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					
				}
			})
			.show();
    }

	public Handler mHandler = new Handler() {
    	public void handleMessage(Message msg) {
    		super.handleMessage(msg);
    		textView_receivedData.setText(content);
    	}
    };
    
    
}
